//
//  WPMinePlaceholderTextView.m
//  wobingwoyi
//
//  Created by WGP on 16/8/6.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPMinePlaceholderTextView.h"

@interface WPMinePlaceholderTextView ()<UITextViewDelegate>

@end

@implementation WPMinePlaceholderTextView

-(instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer
{
    if (self = [super initWithFrame:frame textContainer:textContainer])
    {
        [self prepareUI];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeText) name:UITextViewTextDidChangeNotification object:self];
        
    }
    return self;
}

-(void)prepareUI
{
    [self addSubview:self.placeholderLabel];
    
    self.placeholderLabel.translatesAutoresizingMaskIntoConstraints = false;
//    [self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self).offset(3);
//        make.top.equalTo(self).offset(8);
//        make.right.equalTo(self).offset(-3);
//    }];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGSize size = [self.placeholder sizeWithMaxSize:CGSizeMake(self.bounds.size.width-6, MAXFLOAT) systemFont:self.font];
    self.placeholderLabel.frame = CGRectMake(3, 8, self.bounds.size.width-6, size.height+2);
}

-(UILabel *)placeholderLabel
{
    if (_placeholderLabel == nil)
    {
        _placeholderLabel = [[UILabel alloc] init];
        _placeholderLabel.font = self.font;
        _placeholderLabel.numberOfLines = 0;
        _placeholderLabel.textColor = RGB_COLOR(208, 208, 213);
        
        _placeholderLabel.text = @"写下您宝贵的建议和意见";
        [_placeholderLabel sizeToFit];
    }
    return _placeholderLabel;
}

-(void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    self.placeholderLabel.text = placeholder;
    
//    [self layoutIfNeeded];
}

-(void)setText:(NSString *)text
{
    [super setText:text];
    self.placeholderLabel.hidden = [self hasText];
}

-(void)setFont:(UIFont *)font
{
    [super setFont:font];
    self.placeholderLabel.font = font;
}

-(void)didChangeText
{
    self.placeholderLabel.hidden = [self hasText];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end





